﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerCliente: Broker
    {
        #region constructor

        public BrokerCliente()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {            
            Cliente clienteAux = (Cliente)o;
            this.conn.connect();
            string sql = "insert into Personas(Nombre, Direccion, IdPlan, Habilitado, Fax, EMail, Saldo," 
                + " EsCliente, RUT) values(" 
                + "'" + clienteAux.Nombre + "', '" + clienteAux.Direccion + "', " 
                + clienteAux.PlanDePago.OID + ", " + clienteAux.Habilitado + ", " + clienteAux.Fax + ", '" 
                + clienteAux.EMail + "', '" + clienteAux.Saldo + "', true, '" + clienteAux.RUT + "')";
            this.conn.execute(sql);            
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Cliente clienteAux = (Cliente)o;
            string sql = "update Personas set Nombre = '" + clienteAux.Nombre
                + "', Direccion = '" + clienteAux.Direccion
                + "', IdPlan = " + clienteAux.PlanDePago.OID
                + ", Habilitado = " + clienteAux.Habilitado
                + ", Fax = " + clienteAux.Fax
                + ", EMail = '" + clienteAux.EMail
                + "', Saldo = '" + clienteAux.Saldo
                + "', Rut = '" + clienteAux.RUT
                + "' where IdPersona = " + clienteAux.OID;
            this.conn.connect();
            this.conn.execute(sql);            
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Cliente clienteAux = (Cliente)o;
            string sql = "update Personas set Habilitado = false where idPersona = " + clienteAux.OID; 
            this.conn.connect();
            this.conn.execute(sql);            
            this.conn.disconnect();
        }
        
        public override void bringData(Object o)
        {
            Cliente clienteAux = (Cliente)o;
            this.conn.connect();
            string sql = "Select p.* from Personas " 
                + "where p.IdPersona = " + clienteAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {                
                clienteAux.Nombre = this.conn.getDataString("Nombre").ToString();
                clienteAux.Direccion = this.conn.getDataString("Direccion").ToString();
                clienteAux.PlanDePago.OID = int.Parse(this.conn.getDataString("IdPlan").ToString());
                clienteAux.Habilitado = (Boolean)this.conn.getDataString("Habilitado");
                clienteAux.Fax = int.Parse(this.conn.getDataString("Fax").ToString());
                clienteAux.EMail = this.conn.getDataString("EMail").ToString();
                clienteAux.Saldo = double.Parse(this.conn.getDataString("Saldo").ToString());                
                clienteAux.RUT = this.conn.getDataString("Rut").ToString();
            }                      
            this.conn.disconnect();            
        }
        
        public override List<Object> bringList(Object o)
        {
            string sql = "Select p.*, pp.nombre as nombrePlan, pp.CantidadDias, pp.Descuento" 
                + " from Personas p, PlanesDePago pp" 
                + " where p.Habilitado = true"
                + " and p.idPlan = pp.idPlan"
                + " and esCliente = true";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Cliente clienteAux = new Cliente();
                clienteAux.Nombre = this.conn.getDataString("Nombre").ToString();
                clienteAux.Direccion = this.conn.getDataString("Direccion").ToString();
                clienteAux.PlanDePago.OID = int.Parse(this.conn.getDataString("IdPlan").ToString());
                clienteAux.PlanDePago.Nombre = this.conn.getDataString("nombrePlan").ToString();
                clienteAux.PlanDePago.CantDias = int.Parse(this.conn.getDataString("CantidadDias").ToString());
                clienteAux.PlanDePago.Descuento = int.Parse(this.conn.getDataString("Descuento").ToString());
                clienteAux.Habilitado = (Boolean)this.conn.getDataString("Habilitado");
                clienteAux.Fax = int.Parse(this.conn.getDataString("Fax").ToString());
                clienteAux.EMail = this.conn.getDataString("EMail").ToString();
                clienteAux.Saldo = double.Parse(this.conn.getDataString("Saldo").ToString());
                clienteAux.OID = int.Parse(this.conn.getDataString("IdPersona").ToString());                
                clienteAux.RUT = this.conn.getDataString("Rut").ToString();
                list.Add(clienteAux);
            }            
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            try
            {
                this.conn.connect();
                string sql = "Select max(idPersona)as id from Personas";
                this.conn.bringData(sql);
                while (this.conn.nextRecord())
                {
                    id = int.Parse(this.conn.getDataString("id").ToString());
                }
                this.conn.disconnect();
            }
            catch (Exception)
            {
                this.conn.disconnect();
                id = 0;
            }            
            return id;
        }

        #endregion        
    }
}
